package com.heima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.feign.ArticleFeign;
import com.heima.behavior.dto.FollowBehaviorDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.UserEnum;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.user.dto.UserRelationDto;
import com.heima.user.entity.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.IApUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-03
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Value("${topic.followBehaviorTopic}")
    private String followBehaviorTopic;

    /**
     * 关注作者
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1.构建关注用户对象,属性赋值
        User user = UserThreadLocalUtil.get();
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setLevel(UserEnum.USER_FOLLOW_LEVEL_ONCE.getVal());
        //1.1 设置登录用户信息
        apUserFollow.setUserId(user.getUserId());
        apUserFollow.setUserName(user.getUserName());
        //1.2 设置关注目标用户信息
        ResponseResult<ApAuthor> responseResult = articleFeign.getAuthorById(dto.getAuthorId());
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor apAuthor = responseResult.getData();
            LambdaQueryWrapper<ApUserFollow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserFollow::getUserId, user.getUserId());
            queryWrapper.eq(ApUserFollow::getFollowId, apAuthor.getUserId());
            if (dto.getOperation().equals(UserEnum.USER_FOLLOW_OPRATION.getVal())) {
                //关注操作
                apUserFollow.setFollowId(apAuthor.getUserId());
                apUserFollow.setFollowName(apAuthor.getName());
                //2.执行保存/删除
                ApUserFollow one = this.getOne(queryWrapper);
                if (one == null) {
                    this.save(apUserFollow);
                }


            } else {
                //取消关注
                this.remove(queryWrapper);
            }
            //3.TODO 发送关注/取关行为消息
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setEquipmentId(dto.getEquipmentId());
            followBehaviorDto.setUserId(user.getUserId());
            followBehaviorDto.setFollowId(apAuthor.getUserId());
            followBehaviorDto.setOperation(dto.getOperation());
            kafkaTemplate.send(followBehaviorTopic, JSON.toJSONString(followBehaviorDto));
        }
        return ResponseResult.okResult();
    }
}
